package ch.fhnw.kosmonauten.client.gui;

import java.util.Locale;

import javax.swing.JOptionPane;

import ch.fhnw.kosmonauten.client.net.ServerConnection;
import ch.fhnw.kosmonauten.model.User;
import ch.fhnw.kosmonauten.net.LoginRequest;
import ch.fhnw.kosmonauten.net.Request;
import ch.fhnw.kosmonauten.net.Response;
import ch.fhnw.kosmonauten.server.dal.UserAccessLayer;

public class UserFrame extends javax.swing.JFrame {
	
	private ServerConnection sc;
	
	public UserFrame(User user, String mode) {
        this.user = user;
        this.toFront();
		this.setAlwaysOnTop(true);
		initComponents();
		this.jTextField1.setEnabled(false);
		if (mode.equals("user"))
			this.jRadioButton2.setEnabled(false);
		this.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        jTextField3 = new javax.swing.JTextField();

        setTitle(user.displayValue("mw106"));

        jButton1.setText(user.displayValue("mw107"));

        jLabel1.setText(user.displayValue("mw108"));

        jLabel2.setText(user.displayValue("mw109"));

        jLabel3.setText(user.displayValue("mw110"));

        jLabel4.setText(user.displayValue("mw111"));

        jTextField1.setText(user.getUsername());

        buttonGroup2.add(jRadioButton1);
        jRadioButton1.setText(user.displayValue("mw112"));

        buttonGroup2.add(jRadioButton2);
        jRadioButton2.setText(user.displayValue("mw113"));

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText(user.displayValue("mw114"));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            @Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton4);
        jRadioButton4.setText(user.displayValue("mw115"));

        jTextField3.setText(user.getPassword());
        
        if (user.getLanguage() == Locale.GERMAN){
        	jRadioButton3.setSelected(true);
        }
        else if (user.getLanguage() == Locale.ENGLISH){
        	jRadioButton4.setSelected(true);
        }
        
        if (user.getRole().equalsIgnoreCase("User")){
        	jRadioButton1.setSelected(true);
        }
        else if (user.getRole().equalsIgnoreCase("Admin")){
        	jRadioButton2.setSelected(true);
        }

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton4)
                            .addComponent(jRadioButton3)
                            .addComponent(jRadioButton1)
                            .addComponent(jRadioButton2)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                            .addComponent(jTextField3)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jRadioButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton4)
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jRadioButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton2)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

		if (jTextField1.getText().equalsIgnoreCase(user.getUsername()) && 
				jTextField3.getText().length() > 0 && 
				(jRadioButton1.isSelected() || jRadioButton2.isSelected()) && 
				(jRadioButton3.isSelected() || jRadioButton4.isSelected())) 
		{
			user.setPassword(jTextField3.getText());
			if (jRadioButton1.isSelected()){
				user.setRole("user");
			}
			else{
				user.setRole("admin");
			}
			if (jRadioButton3.isSelected()){
				user.setLanguage(Locale.GERMAN);
			}
			else{
				user.setLanguage(Locale.ENGLISH);
			}
			// User profil speichern
			sc = new ServerConnection();
			Response resp = sc.sendRequest(new Request(this.user,"saveUserProfile"));
			if (resp == null)
				System.out.println(user.displayValue("mw82"));
			else if (resp.getContent() instanceof Boolean) 
			{
				boolean ok = (Boolean) resp.getContent();
				if (ok)
				{
					this.setVisible(false);
				}
				else
				{
					new ErrorDialog(4, JOptionPane.ERROR_MESSAGE, user);
				}
			}
			else if (resp.getContent() == null )
			{
				new ErrorDialog(5, JOptionPane.ERROR_MESSAGE, user);
			}
			else
				System.out.println(resp.getContent().toString());
			//UserAccessLayer ual = new UserAccessLayer();
			//ual.saveUser(user);
			
		} else
			new ErrorDialog(6, JOptionPane.INFORMATION_MESSAGE, user);

	}


    // Variables declaration - do not modify
    private User user;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration
	

}
